<?php


namespace App\Http\Controllers\Admin;

use App\Modules\User\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;

class TemplateController extends Controller
{
    public function index(Request $request)
    {
        $this->authorize('template');
        $type = $request->input('type', 0);

        $templates = DB::table('templates')
            ->where("type", $type)
            ->orderBy("sort", "desc")
            ->get();
        return view('template.index', compact('templates', 'type'));
    }

    public function changeSort(Request $request)
    {
        $this->authorize('template-edit');
        $id = $request->input('id');
        $sort = $request->input('sort');
        DB::table('templates')
            ->where('id', $id)
            ->update(['sort' => $sort]);
        return redirect()->back();
    }

    public function edit($id, Request $request)
    {
        $this->authorize('template-edit');

        $template = DB::table('templates')->where("id", $id)->first();
        $data = json_decode($template->json, true);
        $template->first = $data['first'];
        $template->remark = $data['remark'];

        unset($data['first']);
        unset($data['remark']);
        session()->put('url.intended', url()->previous());
        return view('template.update', compact('template', 'data'));

    }

    public function create(Request $request)
    {
        $this->authorize('template-add');
        $type = $request->input('type', 0);
        session()->put('url.intended', url()->previous());
        return view('template.create', compact('type'));
    }

    public function store(Request $request)
    {
        $this->authorizeAny(['template-add', 'template-']);
        $aPost = $request->input();

        if (isset($aPost['id'])) {
            // 修改
            $id = (int)$aPost['id'];

            $template = DB::table('templates')->where("id", $id)->first();
            $data = json_decode($template->json, true);
            $data['first'] = $aPost['first'];
            $data['remark'] = $aPost['remark'];

            DB::table('templates')->where('id', '=', $id)->update(['json' => json_encode($data)]);

        } else {
            // 添加
            $aTemplate = $aKeyWord = $aData = $aData1 = [];
            $aData['first'] = $aData1['first'] = $aPost['first'];

            for ($i = 0; $i < count($aPost['keyword_key']); $i++) {
                $aData[$aPost['keyword_key'][$i]] = $aPost['keyword_value'][$i];
                $k = 'keyword' . ($i + 1);
                $aKeyWord[$k] = $aPost['keyword_key'][$i];
                $aData1[$k] = $aPost['keyword_value'][$i];
            }

            $aData['remark'] = $aData1['remark'] = $aPost['remark'];

            $aTemplate['scene'] = $aPost['scene'];
            $aTemplate['template_id'] = $aPost['template_id'];
            $aTemplate['json'] = json_encode($aData);
            $aTemplate['type'] = (int)$aPost['type'];

            DB::table('templates')->insert($aTemplate);
        }
        return redirect()->intended();
        // return redirect()->route('admin.template.index');
    }

    /**
     * 模板消息开关
     * @param $id
     */
    public function status($id)
    {
        $this->authorize('template_on_off');

        $template = DB::table('templates')->where('id', $id)->first();

        if ($template->status == 1) {
            DB::table('templates')->where('id', $id)->update([
                'status' => 0,
            ]);
        } else {
            DB::table('templates')->where('id', $id)->update([
                'status' => 1,
            ]);
        }
        return redirect()->route('admin.template.index');
    }

}
